package com.joinlabs.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import com.joinlabs.entity.Address;
import com.joinlabs.entity.Enshrine;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Wares;
import com.joinlabs.exception.CustomException;
import com.joinlabs.mapper.AddressMapper;
import com.joinlabs.mapper.EnshrineMapper;
import com.joinlabs.mapper.UserMapper;
import com.joinlabs.mapper.WaresMapper;
import com.joinlabs.service.IUserService;
import com.joinlabs.util.MD5Utils;

@Service("userService")
public class UserServiceImpl implements IUserService {

	@Resource
	private UserMapper userMapper;
	@Resource
	private AddressMapper addressMapper;
	@Resource
	private EnshrineMapper enshrineMapper;
	@Resource
	private WaresMapper waresMapper;

	//注册方法
	@Override
	public void userregister(String userId, String userName, String userPassword, String userSurname, String userPhone,
			String userAddress, String bankId, int userPart, String userGender, long userAge, String userQuestion,
			String userAnswer, String userImg) {
			//创建用户对象
			User user = new User();
			//将值封装进用户对象
			user.setUserId(userId);//用户id兼身份证号
			user.setUserName(userName);//用户名
			user.setUserPassword(userPassword);//用户密码
			user.setUserSurname(userSurname);//真实姓名
			user.setUserPhone(userPhone);//手机号码
			user.setUserAddress(userAddress);//住址
			user.setBankId(bankId);//银行卡号
			user.setUserPart(userPart);
			user.setUserGender(userGender);
			user.setUserAge(userAge);//年龄
			user.getUserQuestion();//密保问题
			user.setUserAnswer(userAnswer);//密保答案
			user.setUserImg(userImg);//用户头像
			//调用插入语句执行方法
			userMapper.insertSelective(user);
	}
	
	//判断用户是否存在的方法
	@Override
	public User select(String userName) {
		User user = userMapper.selectByPrimaryKey(userName);
		return user;
	}

	//登录方法
	@Override
	public User selectuserlogin(String userPhone, String userPassword,int userPart) {
		User user = userMapper.selectlogin(userPhone);
		if(user != null){
			if(user.getUserPassword().equals(userPassword) && (user.getUserPart() == 1)){
				return user;
			}
		}
		return null;
	}
	@Override
	public User selectadminlogin(String userPhone, String userPassword,int userPart) {
		User user = userMapper.selectlogin(userPhone);
		if(user != null){
			if(user.getUserPassword().equals(userPassword) && (user.getUserPart() == 0)){
				return user;
			}
		}
		return null;
	}

	@Override
	public User checkLogin(User user) throws CustomException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	//验证手机号
	public boolean verPhone(String phone) {
		//维护一个用户对象
		User user = userMapper.verPhone(phone);
		
		//判断user是否存在
		if(user != null) {
			//存在
			return true;
		}
		//不存在
		return false;
	}

	@Override
	public boolean verUsername(String username) {
		//维护一个用户对象
		User user = userMapper.verUsername(username);
		
		//判断user是否存在
		if(user != null) {
			//存在
			return true;
		}
		//不存在
		return false;
	}

	@Override
	public boolean verAnswer(String answer,String phone) {
		//维护一个用户对象
		String userAnswer = userMapper.verAnswer(phone);
		
		//判断答案是否正确
		if(userAnswer.equals(answer)) {
			//正确
			return true;
		}
		//不正确
		return false;
	}

	@Override
	public void updateInfo(User user) {
		//更新用户信息
		userMapper.updateInfo(user);
	}
	
	//判断手机号是否已被使用
		@Override
		public User checkphone(String userPhone) {
			User user = userMapper.checkPhone(userPhone);
			return user;
		}
		
		//忘记密码功能
		@Override
		public void setpassword(String userPhone, String userPassword) {
			System.out.println("222222222222222222222222222222222");
			System.out.println(userPassword);
			User user = new User();
			user.setUserPhone(userPhone);
			user.setUserPassword(userPassword);
			userMapper.setpassword(user);
		}
	
	@Override
	public void updateUserImg(String fileName, User user) {
		//更新用户头像信息
		userMapper.updateUserImg(fileName, user.getUserId());
	}

	@Override
	public List<Address> selectAddressByUserId(String userId) {
		//查询用户收获地址
		List<Address> list = addressMapper.selectAddressByUserId(userId);
		
		return list;
	}
	
	public int deleteAddrByAid(Long zyAid) {
		//删除收货地址
		int row = addressMapper.deleteByPrimaryKey(zyAid);
		
		return row;
	}

	@Override
	public void addAddressByUserId(Address address) {
		//添加用户收货地址
		addressMapper.insert(address);
	}

	@Override
	public void updateAddressById(Address address) {
		//更新用户收货地址
		addressMapper.updateByPrimaryKey(address);
	}

	@Override
	public List<Wares> selectEnshrineByUserId(String userId) {
		//通过用户查询收藏列表
		List<Enshrine> list = enshrineMapper.selectEnshrineByUserId(userId);
		//维护一个商品Id数组
		//BigDecimal[] waresIds = new BigDecimal[];
		
		List<Wares> waresList = new ArrayList<Wares>();
		
		for (Enshrine enshrine : list) {
			waresList.add(waresMapper.selectByPrimaryKey(enshrine.getWaresId()));
		}
		return waresList;
	}

	@Override
	public int deleteCollect(String userId, BigDecimal waresId) {
		//通过用户和商品查询收藏Id
		Long enshrineId = enshrineMapper.selectEnshrineId(userId, waresId);
		//通过收藏Id取消收藏
		int row = enshrineMapper.deleteByPrimaryKey(enshrineId);
		
		return row;
	}

}
